import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);
/* Layout */
import Layout from "@/layout";
import { authJudgment, getUser } from "@/utils/appUtil";
import {
  getToken,
  setToken,
  removeToken,
  setUserinfo,
  getUserinfo,
  removeUser,
} from "@/utils/auth";



export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  { path: "*", redirect: "/404", hidden: true },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard/index",
    children: [
      {
        path: "/dashboard/index",
        name: "数据概览",
        component: () => import("@/views/dashboard/index"),
        meta: {
          title: "数据概览",
          icon: "el-icon-s-data",
          roles: ["manage", "business", "business_agency"],
        },
      },
    ],
  },
  {
    path: "/operate",
    component: Layout,
    redirect: "/operate",
    name: "operate",
    meta: {
      title: "运营管理",
      icon: "el-icon-data-analysis",
      roles: ["manage", "business", "business_agency"],
    },
    children: [
      {
        path: "/operate/clue",
        name: "线索列表",
        component: () => import("@/views/operate/clue"),
        meta: {
          title: "线索列表",
          activeMenu: "/operate/clue",
          roles: ["manage", "business", "business_agency"],
        },
      },
      {
        path: "/operate/clueMatchingRecord",
        name: "线索匹配记录",
        component: () => import("@/views/operate/clueMatchingRecord"),
        meta: {
          title: "线索匹配记录",
          activeMenu: "/operate/clueMatchingRecord",
          roles: ["manage", "business", "business_agency"],
        },
      },
      {
        path: "/operate/blacklist",
        name: "黑名单管理",
        component: () => import("@/views/operate/blacklist"),
        meta: {
          title: "黑名单管理",
          activeMenu: "/operate/blacklist",
          roles: ["manage", "business", "business_agency"],
        },
      },
      {
        path: "/operate/contract",
        name: "合同管理",
        component: () => import("@/views/operate/contract"),
        meta: {
          title: "合同管理",
          activeMenu: "/operate/contract",
          roles: ["manage"],
        },
      },
      {
        path: "/operate/contractUpdate",
        name: "合同更新",
        component: () => import("@/views/operate/contractUpdate"),
        meta: {
          title: "合同更新",
          activeMenu: "/operate/contract",
          roles: ["manage"],
        },
        hidden: true,
      },
    ],
  },
  // {
  //   path: "/agent",
  //   component: Layout,
  //   redirect: "/index",
  //   children: [
  //     {
  //       path: "index",
  //       name: "人员管理",
  //       component: () => import("@/views/agent/index"),
  //       meta: { title: "人员管理", icon: "jingliren", roles: ["manage"] },
  //     },
  //   ],
  // },
  {
    path: "/organization",
    component: Layout,
    redirect: "/organization/index",
    name: "organization",
    meta: {
      title: "客户/机构管理",
      icon: "el-icon-office-building",
      roles: ["manage", "business", "business_agency"],
    },
    children: [
      {
        path: "agent",
        name: "客户管理",
        component: () => import("@/views/agent/index"),
        meta: {
          title: "客户管理",
          roles: ["manage"],
        },
      },
      {
        path: "index",
        name: "机构列表",
        component: () => import("@/views/organization/index"),
        meta: {
          title: "机构列表",
          roles: ["manage", "business", "business_agency"],
        },
      },
      {
        path: "/organizationMutex/index",
        name: "机构互斥",
        component: () => import("@/views/organizationMutex/index"),
        meta: {
          title: "机构互斥",
          roles: ["manage"],
        },
      },
      {
        path: "/organizationMutex/create",
        name: "添加机构互斥",
        component: () => import("@/views/organizationMutex/create"),
        meta: {
          title: "添加机构互斥",
          roles: ["manage"],
        },
        hidden: true,
      },
      {
        path: "/organizationMutex/update",
        name: "添加机构互斥",
        component: () => import("@/views/organizationMutex/create"),
        meta: {
          title: "修改机构互斥",
          roles: ["manage"],
        },
        hidden: true,
      },
      {
        path: "/dashboard/productStats",
        name: "助贷收益统计",
        component: () => import("@/views/dashboard/productStats"),
        meta: {
          title: "助贷收益统计",
          roles: ["manage", "business", "business_agency"],
        },
      },
      {
        path: "check",
        name: "审核",
        component: () => import("@/views/organization/check"),
        meta: {
          title: "机构审核",
          roles: ["manage", "business", "business_agency"],
        },
      },
      {
        path: "/organization/create",
        name: "添加",
        component: () => import("@/views/organization/create"),
        meta: {
          title: "机构添加",
          activeMenu: "/organization/index",
          roles: ["manage", "business", "business_agency"],
        },
        hidden: true,
      },
      {
        path: "/organization/update",
        name: "修改",
        component: () => import("@/views/organization/create"),
        meta: {
          title: "机构修改",
          activeMenu: "/organization/index",
          roles: ["manage", "business", "business_agency"],
        },
        hidden: true,
      },
      {
        path: "/organization/transaction",
        name: "入账",
        component: () => import("@/views/organization/transaction"),
        meta: {
          title: "机构入账",
          activeMenu: "/organization/index",
          roles: ["manage", "business",'business_agency'],
        },
        hidden: true,
      },
      {
        path: "/organization/bill",
        name: "机构账单",
        component: () => import("@/views/organization/bill"),
        meta: {
          title: "机构账单",
          roles: ["manage", "business", "business_agency"],
        },
      },
    ],
  },
  {
    path: "/product",
    component: Layout,
    redirect: "/product/index",
    name: "product",
    meta: {
      title: "产品管理",
      icon: "el-icon-shopping-bag-2",
      roles: ["manage", "business", "business_agency"],
    },
    children: [
      {
        path: "index",
        name: "产品列表",
        component: () => import("@/views/product/index"),
        meta: {
          title: "产品列表",
          activeMenu: "/product/index",
          roles: ["manage", "business", "business_agency"],
        },
      },
      {
        path: "create",
        name: "产品添加",
        component: () => import("@/views/product/create"),
        meta: {
          title: "产品添加",
          activeMenu: "/product/index",
          roles: ["manage", "business", "business_agency"],
        },
        hidden: true,
      },
      {
        path: "update",
        name: "产品修改",
        component: () => import("@/views/product/create"),
        meta: {
          title: "产品修改",
          activeMenu: "/product/index",
          roles: ["manage", "business", "business_agency"],
        },
        hidden: true,
      },
      {
        path: "clue",
        name: "产品线索",
        component: () => import("@/views/product/clue"),
        meta: {
          title: "产品线索",
          activeMenu: "/product/clue",
          roles: ["manage"],
        },
      },
    ],
  },
 
  {
    path: "/advertising",
    component: Layout,
    redirect: "/advertising/index",
    name: "advertising",
    meta: { title: "贷超管理", icon: "el-icon-present", 
      roles: ["manage"] },
    children: [
      {
        path: "/advertising/index",
        name: "贷超列表",
        component: () => import("@/views/advertising/index"),
        meta: {
          title: "贷超列表",
          activeMenu: "/advertising/index",
          roles: ["manage"],
        },
      },
      {
        path: "/dashboard/advertisingStats",
        name: "贷超统计",
        component: () => import("@/views/dashboard/advertisingStats"),
        meta: {
          title: "贷超统计",
          activeMenu: "/dashboard/advertisingStats",
          roles: ["manage"],
        },
      },
      {
        path: "/advertising/create",
        name: "贷超添加",
        component: () => import("@/views/advertising/create"),
        meta: {
          title: "贷超添加",
          activeMenu: "/advertising/index",
          roles: ["manage"],
        },
        hidden: true,
      },
      {
        path: "/advertising/update",
        name: "贷超修改",
        component: () => import("@/views/advertising/create"),
        meta: {
          title: "贷超修改",
          activeMenu: "/advertising/index",
          roles: ["manage"],
        },
        hidden: true,
      },
      {
        path: "/advertising/recharge",
        name: "贷超充值",
        component: () => import("@/views/advertising/transaction"),
        meta: {
          title: "贷超充值",
          activeMenu: "/advertising/index",
          roles: ["manage"],
        },
        hidden: true,
      },
      {
        path: "/advertising/expense",
        name: "贷超退款",
        component: () => import("@/views/advertising/transaction"),
        meta: {
          title: "贷超退款",
          activeMenu: "/advertising/index",
          roles: ["manage"],
        },
        hidden: true,
      },
    ],
  },
  {
    path: "/channel",
    component: Layout,
    redirect: "/channel/index",
    name: "channel",
    meta: { title: "渠道管理", icon: "el-icon-connection", roles: ["manage"] },
    children: [
      {
        path: "/channel/index",
        name: "渠道管理",
        component: () => import("@/views/channel/index"),
        meta: {
          title: "渠道管理",
          activeMenu: "/channel/index",
          roles: ["manage"],
        },
      },
      {
        path: "/dashboard/channelStats",
        name: "渠道数据统计",
        component: () => import("@/views/dashboard/channelStats"),
        meta: {
          title: "渠道数据统计",
          activeMenu: "/dashboard/channelStats",
          roles: ["manage"],
        },
      },
      {
        path: "create",
        name: "渠道添加",
        component: () => import("@/views/channel/create"),
        meta: {
          title: "渠道添加",
          activeMenu: "/channel/index",
          roles: ["manage"],
        },
        hidden: true,
      },
      {
        path: "update",
        name: "渠道修改",
        component: () => import("@/views/channel/create"),
        meta: {
          title: "渠道修改",
          activeMenu: "/channel/index",
          roles: ["manage"],
        },
        hidden: true,
      },
      {
        path: "transaction",
        name: "渠道入账",
        component: () => import("@/views/channel/transaction"),
        meta: {
          title: "渠道入账",
          activeMenu: "/channel/transaction",
          roles: ["manage"],
        },
        hidden: true,
      },
    ],
  },
  {
    path: "/system",
    component: Layout,
    redirect: "/system",
    meta: {
      title: "日志管理",
      icon: "el-icon-tickets",
      roles: ["manage"],
    },
    children: [
      {
        path: "loginLog",
        name: "登录日志",
        component: () => import("@/views/system/loginLog"),
        meta: { title: "登录日志", roles: ["manage"] },
      }
      ,{
        path: "/system/manageLogs",
        name: "管理员操作日志",
        component: () => import("@/views/system/manageLogs"),
        meta: {
          title: "管理员操作日志",
          activeMenu: "/system/manageLogs",
          roles: ["manage"],
        },
      },
    ],
  },
];

export function fuConstantRoutes(group_key = "") {
  if (!group_key) {
    var userinfo = getUser();
    group_key = userinfo["group_key"];
  }
  var userConstantRoutes = filterRoutes(constantRoutes, group_key);
  return userConstantRoutes;
}

export function filterRoutes(routes, role) {
  // 创建一个新的数组用于存放符合条件的路由
  const filteredRoutes = [];

  // 遍历每一个路由
  for (let i = 0; i < routes.length; i++) {
    const route = routes[i];
    const newRoute = { ...route }; // 克隆路由对象

    // 检查当前路由的 path 是否为 /login 或 /404
    if (route.path === "/login" || route.path === "/404") {
      filteredRoutes.push(route);
      continue;
    }

    // 检查是否有 meta.roles 属性
    if (route.meta && route.meta.roles) {
      // 检查当前角色是否在 meta.roles 中
      if (route.meta.roles.includes(role)) {
        // 如果有 children 属性，递归处理子路由
        if (route.children && route.children.length > 0) {
          newRoute.children = filterRoutes(route.children, role);
        }
        filteredRoutes.push(newRoute);
      }
    } else {
      // 如果没有 meta.roles 属性，处理子路由
      if (route.children && route.children.length > 0) {
        newRoute.children = filterRoutes(route.children, role);
      }
      filteredRoutes.push(newRoute);
    }
  }

  return filteredRoutes;
}

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: fuConstantRoutes(),
  });

const router = createRouter();

export function resetRouter(group_key) {
  const newRouter = new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: fuConstantRoutes(group_key),
  });
  router.options = newRouter.options; // reset router
  router.matcher = newRouter.matcher; // reset router
  // console.log(router.options)
}

export default router;
